/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toolutils;

import datadownapplication.DataDownView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Administrator
 */
public class StringUtils {
    //fhzww
    public static void InsertChapter(String chapterurl)
    {
        String htmlcode = ToolUtils.getContent(chapterurl);
        if(htmlcode!=null)
        {
                String a[] = htmlcode.split("class=\"list\">");
                if(a.length<2)
                {
                    System.out.println("a length < 2");
                    return;
                }
                String b[] = a[1].split("</table>");
                if(b.length<2)
                {
                    System.out.println("b lenght < 2");
                    return;
                }
                String c[] = b[0].split("<td>");
                if(c.length<2)
                {
                    System.out.println("c lenght < 2");
                    return;
                }else{
                    for(int i=1;i<c.length;i++)
                    {
                        String d[] = c[i].split("</td>");
                        if(d.length<2)
                        {
                            System.out.println("d lenght < 2");
                            return;
                        }
                        String content = d[0];
                        String pattern= "href=\"([^\"]*)\"";
                        Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
                        Matcher m = p.matcher(content);
                        if(m.find()) {
                             System.out.println("url="+m.group(1));
                        }

                        pattern = "title=\"([^\"]*)\"";
                        p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
                        m = p.matcher(content);
                        if(m.find()) {
                             System.out.println("title="+m.group(1));
                        }
                    }
                }
        }else{
            System.out.println("htmlcode is null");
        }    
    }

    //DUDU8
    public static void ChapterInsert(String chapterurl,int tableid,DataDownView aThis,boolean hascontent)
    {//<div class="book_article_texttable"> <div class="footer>        
        String htmlcode = ToolUtils.getContent(chapterurl);
        if(htmlcode!=null)
        {
                String a[] = htmlcode.split("book_article_texttable");
                if(a.length<2)
                {
                    System.out.println("a length < 2");
                    return;
                }
                String b[] = a[1].split("<div class=\"footer");
                if(b.length<2)
                {
                    System.out.println("b lenght < 2");
                    return;
                }
                String c[] = b[0].split("<dd>");
                if(c.length<2)
                {
                    System.out.println("c lenght < 2");
                    return;
                }else{
                    
                    int from = DbManager.Intance().getTableCount(tableid);                    
                    aThis.initProgressBar(from,c.length);
                    for(int i=from+1;i<c.length;i++)
                    {
                        String d[] = c[i].split("</dd>");
                        //System.out.println(d[0]);
                        if(d.length<2)
                        {
                            System.out.println("d lenght < 2");
                            return;
                        }

                        if(d[0].contains("href="))
                        {
                            String e[] = d[0].split("href=\"");
                            if(e.length<2)
                            {
                                if(e.length==0)
                                {
                                    continue;
                                }
                                System.out.println("e lenght < 2");
                                return;
                            }
                            String f[] = e[1].split("\">");
                            if(f.length<2)
                            {
                                System.out.println("f lenght < 2");
                                return;
                            }
                            String curl = chapterurl+f[0];
                            //System.out.println("url:"+curl);
                            String g[] = f[1].split("</a>");
                            //System.out.println("title:"+g[0]);
                            String ctitle = g[0];
                            String content = null;
                            if(hascontent)
                            {
                                content = GetContent(curl);
                            }
                            int insertid = DbManager.Intance().InsertBTable(tableid, ctitle, curl,content);
                            if(insertid!=-1)
                            {
                                System.out.println("插入成功："+insertid);
                                aThis.setProgressBar(i, c.length-1);
                            }else{
                                System.out.println("插入失败："+ctitle);
                                break;
                            }
                        }
                        
                    }
                }
        }else{
            System.out.println("htmlcode is null");
        }
    }


    public static String GetContent(String url)
    {
        String content = null;
        String htmlcode = ToolUtils.getContent(url);

        if(htmlcode!=null)
        {
            if(htmlcode.contains(".png")||htmlcode.contains(".jgp"))
            {
                System.out.println("有图...");
            }
            String aa[] = htmlcode.split("<div id=\"booktext\">");
            if(aa.length<2)
            {
                System.out.println("aa lenght < 2");
                return content;
            }
            String bb[] = aa[1].split("</div>");
            if(bb.length<2)
            {
                System.out.println("bb lenght < 2");
                return content;
            }
            String cc[] = bb[0].split("</STRONG>");
            if(cc.length<2)
            {
                System.out.println("cc lenght < 2");
                return content;
            }
            content = cc[1];
            content.replaceAll("武动乾坤 dudu8.net 全文字更新最快的地方", "");
            content.replaceAll("（www.c a i h o n G w e n x u e.c o m，彩 虹 文 学网）", "");
            content.replaceAll("dudu8", "");
        }
        return content;
    }

    


}
